package en.kaist.cs408.acav;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import en.kaist.cs408.R;

public class CreateClass extends Activity {
	
	String courseID;
	String password;
	String password_verif;
	String pwdEncrypt;
	
	static class NetworkHandler extends Handler {
		
	}
	
	public void onCreate(Bundle savedInstanceState) {
     
		super.onCreate(savedInstanceState);
		setContentView(R.layout.createclass);
		
		Button confirmButton = (Button) findViewById(R.id.createclass_confirm);
		confirmButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				/* Check the input's verification */
				Verifier createClassVerifier = new Verifier();
				
				EditText cidEditText = (EditText)findViewById(R.id.createclass_courseID);
				courseID = cidEditText.getText().toString();
				
				EditText pwdEditText = (EditText)findViewById(R.id.createclass_password);
				password = pwdEditText.getText().toString();
				
				EditText pwdVerifEditText = (EditText)findViewById(R.id.createclass_password_verif);
				password_verif = pwdVerifEditText.getText().toString();
				
				boolean cid_result = createClassVerifier.verifyCourseID(courseID);
				int pwd_result = createClassVerifier.verifyPassword(password, password_verif);
				
				if (cid_result == false){
					Toast.makeText(CreateClass.this, "Course ID in WRONG format", Toast.LENGTH_LONG).show();
					cidEditText.requestFocus();
				}
				else if (pwd_result == -1){
					Toast.makeText(CreateClass.this, "Password Verification failed", Toast.LENGTH_LONG).show();
					pwdEditText.requestFocus();
				}
				else if (pwd_result == -2){
					Toast.makeText(CreateClass.this, "Password should be 8 ~ 20 letters", Toast.LENGTH_LONG).show();
					pwdEditText.requestFocus();
				}
				else {
					/* Send to Server (DB update will be occurred in Server-side code)*/
//					Toast.makeText(CreateClass.this, "Send Info to server..", Toast.LENGTH_LONG).show();
					
					SHAEncryption shae = new SHAEncryption();
					pwdEncrypt = shae.encryptSHA256(password);
					
					
					AlertDialog.Builder ad = new AlertDialog.Builder(CreateClass.this);
					ad.setTitle("Alert");
					ad.setMessage("Would you like to make a new class?\n"
							+ "Course ID: " + courseID);
					ad.setNegativeButton("No", null);
					
					ad.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							NetworkHandler handler = new NetworkHandler(){
								public void handleMessage(Message msg){
									switch (msg.what){
									case 0:
										Toast.makeText(CreateClass.this, "Server complete", Toast.LENGTH_LONG).show();
										Intent settingsActivity = new Intent(CreateClass.this, CreateClassSuccess.class);
										startActivity(settingsActivity);
										break;
									case -1:
										Toast.makeText(CreateClass.this, "Course ID duplicated", Toast.LENGTH_LONG).show();
										break;
									case -2:
										Toast.makeText(CreateClass.this, "Server got problem...", Toast.LENGTH_LONG).show();
										break;
									default:
										Toast.makeText(CreateClass.this, "Exception Occurred", Toast.LENGTH_LONG).show();
										break;	
									}
								}
							};
							
							NetworkThread nt = new NetworkThread(3000, courseID, null, pwdEncrypt, null, null,null,null, false, handler);
							nt.start();
						}
					});
					
					ad.show();
					
					
					
				}
				
				
			}
		});
	}
}